//
//  ResignViewController.m
//  囧途
//
//  Created by fly on 16/3/7.
//  Copyright © 2016年 fly. All rights reserved.
//

#import "ResignViewController.h"
#import <AVUser.h>
#import <AVOSCloud.h>
@interface ResignViewController ()
//手机号码
@property (strong, nonatomic) IBOutlet UITextField *userName;
//验证码
@property (strong, nonatomic) IBOutlet UITextField *passWrod;

@property (strong, nonatomic) IBOutlet UIButton *button;

@property (strong, nonatomic) IBOutlet UIButton *phoneButton;
@end

@implementation ResignViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
  }

- (IBAction)phoneButton:(UIButton *)sender {
    
    [AVOSCloud requestSmsCodeWithPhoneNumber:_userName.text callback:^(BOOL succeeded, NSError *error) {
        // 发送失败可以查看 error 里面提供的信息
        if (succeeded) {
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"验证码发送成功请注意查收" preferredStyle:UIAlertControllerStyleAlert];
            
                    UIAlertAction * alertAction =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
                    }];
            
                    [alert addAction:alertAction];
            
                    [self presentViewController:alert animated:YES completion:nil];
        }
        
        else
        {
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入11位手机号码" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction * alertAction =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alert addAction:alertAction];
            
            [self presentViewController:alert animated:YES completion:nil];

        
        }
        
 
    }];
 
    
    
    
}


- (IBAction)buttonPressed:(UIButton *)sender {
    
    [AVUser signUpOrLoginWithMobilePhoneNumberInBackground:_userName.text smsCode:_passWrod.text block:^(AVUser *user, NSError *error) {
        // 如果 error 为空就可以表示登录成功了，并且 user 是一个全新的用户
        NSLog(@"=======%@",error);
        
        
        if (error == nil) {
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功！" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction * alertAction =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                [self.navigationController popToRootViewControllerAnimated:YES];
                
                
                
            }];
            
            [alert addAction:alertAction];
            
            [self presentViewController:alert animated:YES completion:nil];
            
            
        }
        else
        {
        
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"验证码错误！" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction * alertAction =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alert addAction:alertAction];
            
            [self presentViewController:alert animated:YES completion:nil];
        }
        
        
        
    }];
    
    
    
//    if ([_userName.text isEqual: @""]|| [_passWrod.text isEqual: @""]) {
//        
//        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码不能为空！" preferredStyle:UIAlertControllerStyleAlert];
//        
//        UIAlertAction * alertAction =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            
//        }];
//        
//        [alert addAction:alertAction];
//        
//        [self presentViewController:alert animated:YES completion:nil];
//        
//    }
//    
//   else if (_userName.text !=nil && [_passWrod.text isEqualToString:_turePassWord.text]) {
//        
//        NSLog(@"%@",_userName.text);
//        
//        AVUser * user = [AVUser user];
//        
//        user.username = _userName.text;
//        
//        user.password = _passWrod.text;
//        
//        [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
//            
//            if (succeeded) {
//                
//                [AVUser logInWithUsernameInBackground:_userName.text password:_passWrod.text block:^(AVUser *user, NSError *error) {
//                    if (user) {
//                        
//                        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功！" preferredStyle:UIAlertControllerStyleAlert];
//                        
//                        
//                        
//                        UIAlertAction * alertAction =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                            
//                            [self.navigationController popToRootViewControllerAnimated:YES];
//                            
//                            
//                        }];
//                        
//                        [alert addAction:alertAction];
//                        
//                        
//                        
//                        [self presentViewController:alert animated:YES completion:nil];
//                    }
//                }];
//            }
//            
//            else
//                
//            {
//                
//                UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名已存在！" preferredStyle:UIAlertControllerStyleAlert];
//                
//                
//                
//                UIAlertAction * alertAction =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                    
//        }];
//                
//                [alert addAction:alertAction];
//                
//                
//                
//                [self presentViewController:alert animated:YES completion:nil];
//                
//            }
//            
//        }];
//        
//    }
//    
//      else if (![_passWrod.text isEqualToString:_turePassWord.text]) {
//    
//          
//    
//            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码不一致" preferredStyle:UIAlertControllerStyleAlert];
//    
//            
//    
//            UIAlertAction * alertAction =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//    
//                       }];
//    
//            [alert addAction:alertAction];
//    
//            
//    
//            [self presentViewController:alert animated:YES completion:nil];
//    
//        }
    

    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
